<?php
// 申诉处理
class Controller_Admin_Shensu extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type = 'shensu';
        $this->_list_pagesize = 20;
        $this->_no_buttons = TRUE;
        parent::__construct($req, $res);

        $this->_list_where = array(
        );

        $this->_sort_extra_columns = array(
            array('changed', DB::expr('curdate()')),
        );

        $this->_shopId = App_Shop_Manager::currentShopId();
        if ($this->_shopId > 0)
            $this->_list_where[] = array('shopId', '=', $this->_shopId);
    }

	//首页
    public function action_index ()
    {
        $this->action_list();
    }

    
    public function action_list ($page = 1)
    {
        $filter = array();
        $query  = $this->request->query();

        $guider = new Guider;
        $filter['startDay'] = Arr::get($query, 'startDay');
        $filter['stopDay']  = Arr::get($query, 'stopDay');
        $filter['orderNo']  = Arr::get($query, 'orderNo');
        $filter['status']   = Arr::get($query, 'status', -1);
        $filter['shopId']   = Arr::get($query, 'shopId');
        $filter['goodsType']= Arr::get($query, 'goodsType');

        $filter['statusList']    = Order::optionsShensuStatusList();
        $filter['goodsTypeList'] = Goods::optionsGoodsType();
        $filter['shopList']      = $guider->optionsAllGuiders();

        if ($this->_shopId > 0)
            $filter['shopId'] = $this->_shopId;

        if (empty($filter['startDay']))
            $filter['startDay'] = date("Y-m-d", time() - 86400 * 7);

        if (empty($filter['stopDay']))
            $filter['stopDay'] = date("Y-m-d", time());

        // list_where
        if ($filter['status'] >= 0)
            $this->_list_where[] = array('b.shensuStatus', '=', $filter['status']);

        if ($filter['shopId'] > 0)
            $this->_list_where[] = array('a.shopId', '=', $filter['shopId']);

        if (! empty($filter['goodsType']))
            $this->_list_where[] = array('a.goodsType', '=', $filter['goodsType']);

        if (! empty($filter['orderNo']))
        {
            $this->_list_where[] = array('a.orderNo', 'like', '%'.$filter['orderNo'].'%');
        }
        else
        {
            if (! empty($filter['startDay']))
                $this->_list_where[] = array('a.paidTime', '>=', strtotime($filter['startDay']));

            if (! empty($filter['stopDay']))
                $this->_list_where[] = array('a.paidTime', '<', strtotime($filter['stopDay']) + 86400);
        }

        $this->template->set('form_include_head_file', $this->_form->include_edit_file());
        $this->template->set('filter', $filter);

        parent::action_list($page);
    }

    public function action_setShensuStatus ($orderNo)
    {
        $post     = $this->request->post();
        $status   = Arr::get($post, 'status');
        $comments = Arr::get($post, 'comments');

        $order = new Order_Shensu;
        $order->setStatus($orderNo, $status, $comments);

        // 此处要修改，做消息通知
        //Util_Notice::notice_finish_to_guider($orderNo);
        $log = new Order_Log($orderNo);
        $log->add("游客的申诉已受理完毕");
        Util_Notice::notice_refund_backend($orderNo);
        echo json_encode(array('status' => 0));exit;
    }

    public function action_detail ($nid)
    {
        $order = Order::detail($nid);

        // 商家账户
        $guider = Arr::get($order, 'guider');
        $bank   = new UserExt_Bank(Arr::get($guider, 'userUid'));
        $banks  = $bank->getBanKList();

        // 交易信息
        $trans  = new Payment_Transaction(Arr::get($order, 'transactionId'));
        $transDtl = $trans->detail();

        // 结算状态
        $ret = array(
            'shopBillMoney' => empty($order['moneyTotalRMB']) ? '--' : '￥'.number_format($order['moneyTotalRMB']/100, 2),
            'shopBillTime'  => empty($rs['shopBillTime']) ? '--' : date("Y-m-d H:i", $rs['shopBillTime']),
            'shopBillStatus'=> '',
        );
        
        if ($order['shopBillStatus'] == 'READY')
            $ret['shopBillStatus'] = '准备中';
        else if ($order['shopBillStatus'] == 'SUCC')
            $ret['shopBillStatus'] = '已完成';
        else if ($order['shopBillStatus'] == 'FAIL')
            $ret['shopBillStatus'] = '失败';
        else
            $ret['shopBillStatus'] = '未结算';

        // 申诉信息
        $shensu = new Order_Shensu;
        $shensuDetail = $shensu->detail(Arr::get($order, 'id'));


        $this->template->set('title', '结算详细');
        $this->template->set('order', $order);
        $this->template->set('banks', $banks);
        $this->template->set('transaction', $transDtl);
        $this->template->set('shopBill', $ret);
        $this->template->set('shensu', $shensuDetail);
        $this->template->set_filename("smarty:www/shensu/admin_detail");
    }

    public function detail_filter_config_data (& $arr_config)
    {
        //
    }

    private function rebuild_post_data (& $post)
    {
        //
    }

    public function list_filter_data (& $rows)
    {
        foreach ($rows as & $rs)
        {
            // 金额
            $rs['moneyTotal']    = '€' .number_format($rs['moneyTotal']/100, 2);
            $rs['moneyTotalRMB'] = empty($rs['moneyTotalRMB']) ? '--' : '￥'.number_format($rs['moneyTotalRMB']/100, 2);

            // 导游
            $guider = new Guider($rs['shopId']);
            $rs['guiderName']  = $guider->getRealName();
             //游客
            $tourist= new Tourist($rs['uid']);
            $rs['touristName'] = $tourist->getRealName();
            // 类型
            $rs['goodsType'] = Goods::orderGoodsTypeName($rs['goodsType']);

            // 结算
            $rs['shopBillTime']   = empty($rs['shopBillTime']) ? '--' : date("Y-m-d H:i", $rs['shopBillTime']);
        }
    }

    public function list_get_data ($offset, $limit)
    {
        $shensu = new Order_Shensu;
        list($counts, $list) = $shensu->recordsList($offset, $limit, $this->_list_where);
        $this->_list_total_size = $counts;
        return $list;
    }

    public function actions_by_list_row (& $row, & $menus)
    {
        unset($menus[1]);
        unset($menus[2]);

        $status = Arr::get($row, 'shensuStatus');
        if ($status != '已处理')
        {
            $menus[] = "<a href=\"javascript:;\" onClick=\"handle_shop_bill('{$row['orderNo']}')\">处理</a>";
        }
        else
        {
            //
        }
    }
}
